package find_num_of_sensors;



import java.util.Random;

public class Find_Num {

	/**
	 * @param args
	 */
	int x=200;
	int y=200;
	int radius=5;
	int mask[][];
	int board[][];
	int agents[][];

	int sumOfCover;
	int numOfCoverBy3;
	double avg;
	public void createMask()
	{
		int i,j;
		mask=new int[radius*2+1][radius*2+1];
		
		for(i=0;i<(radius*2+1);i++)
		{
			for(j=0;j<(radius*2+1);j++)
			{
				if(Math.sqrt(Math.pow(Math.abs(radius-i),2)+Math.pow(Math.abs(radius-j),2))>radius)
					mask[i][j]=0;
				else
					mask[i][j]=1;
			}
		}
		
		
		/*for(i=0;i<21;i++)
		{
			for(j=0;j<21;j++)
				System.out.print(mask[i][j]+" ");
			System.out.println();
		}*/
		
		
	}
	public void InitBoard(int num_of_agents)
	{
		int i,j;
		board=new int[y+(radius*4)][x+(radius*4)];
		agents=new int[y+radius*2][x+radius*2];
		//int id=1;
		Random rand=new Random();
		int randX, randY;
		for(i=0;i<num_of_agents;i++)
		{
			do{
				randX=rand.nextInt(x+radius*2);
				randY=rand.nextInt(y+radius*2);
			}while(agents[randY][randX]!=0);
			agents[randY][randX]=1;
			//id++;
		}
		/*for(i=0;i<y+radius*2;i++)
		{
			for(j=0;j<x+radius*2;j++)
				System.out.print(agents[i][j]);
			System.out.println();
		}*/
	}
	public void addMask(int y,int x)
	{
		int i,j;
		for(i=0;i<(radius*2+1);i++)
			for(j=0;j<(radius*2+1);j++)
				board[y-radius+i][x-radius+j]+=mask[i][j];
	}
	public void makeCover()
	{
		int i,j;
		for(i=0;i<y+radius*2;i++)
		{
			for(j=0;j<x+radius*2;j++)
			{
				if(agents[i][j]==1)
					addMask(i+radius,j+radius);
			}
		}
		/*for(i=0;i<y+radius*4;i++)
		{
			for(j=0;j<x+radius*4;j++)
				System.out.print(board[i][j]);
			System.out.println();
		}*/
	}
	
	public void countCover()
	{
		int i,j;
		numOfCoverBy3=0;
		for(i=radius*2;i<y+radius*2;i++)
		{
			for(j=radius*2;j<x+radius*2;j++)
			{
				if(board[i][j]>=3)
					numOfCoverBy3++;
				sumOfCover+=board[i][j];
				System.out.print(board[i][j]+" ");
			}
			System.out.println();
		}
		System.out.println("num of covered by 3 "+numOfCoverBy3+" out of "+(x*y)+" "+((double)numOfCoverBy3*100/(x*y)));
		System.out.println("sum of cover "+sumOfCover+" "+((double)sumOfCover/(x*y)));
		
	}
	public boolean check(int numOfagents)
	{
		
		createMask();
		InitBoard(numOfagents);
		makeCover();
		countCover();
		if( ((double)numOfCoverBy3*100/(x*y))>+95)
			return true;
		return false;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Find_Num find;
		int cnt=0;
		find=new Find_Num();
		find.check(3500);
		/*for(int i=50;i<2550;i=i+50)
		//for(int i=450;i<530;i++)
		{
			cnt=0;
			for(int j=0;j<100;j++)
			{
				find=new Find_Num();
				if(find.check(i))
					cnt++;
			}
			if(cnt>=95)
				System.out.println("************"+i+" :GOOD- "+cnt);
			else
				System.out.println("************"+i+" :BAD- "+cnt);
		}*/
	}

}
